package com.haruhinav.controller.admin;

import com.haruhinav.Utils.JWTUtils;
import com.haruhinav.entity.User;
import com.haruhinav.entity.vo.ResultVO;
import com.haruhinav.service.UserService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/admin")
public class AdminLoginController {

    @Resource
    private UserService userService;

    @PostMapping("/x/login")
    public ResultVO login(@RequestBody User user){
        ResultVO result = new ResultVO();
        User findUser =userService.loginFindUser(user.getMid(),user.getPassword());
        if(findUser == null){
            result.setCode(-1);
            result.setMsg("账号或密码错误！");
            result.setType("warning");
        }else if(findUser.getId()!=1){
            result.setCode(-2);
            result.setMsg("你不是管理员！");
            result.setType("warning");
        }else{
            Map<String,Object> datatMap = new HashMap<>();
            Map<String,String> tokenMap = new HashMap<>();
            tokenMap.put("userid",findUser.getId().toString());
            tokenMap.put("usermid",findUser.getMid());

            datatMap.put("user",findUser);
            datatMap.put("token", JWTUtils.createToken(tokenMap));

            result.setCode(0);
            result.setMsg("登录成功");
            result.setData(datatMap);
        }
        return result;
    }

}
